package com.kitchen.service.impl;

import com.kitchen.EntityToVO;
import com.kitchen.common.vo.ResStatus;
import com.kitchen.mapper.FoodLikeMapper;
import com.kitchen.mapper.FoodMapper;
import com.kitchen.beans.entity.Food;
import com.kitchen.beans.entity.FoodLike;
import com.kitchen.beans.vo.FoodVO;
import com.kitchen.beans.vo.LikeVo;
import com.kitchen.common.vo.ResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class IndexService implements com.kitchen.service.IndexService {
    @Autowired
    FoodMapper foodMapper;
    @Autowired
    EntityToVO entityToVO;
    @Autowired
    FoodLikeMapper foodLikeMapper;
    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public ResultVO hotindex() {
        ResultVO resultVO = new ResultVO();
        List<Food> foods = foodMapper.selectAllByFClick();
        List<FoodVO> foodVOS=new ArrayList<>();
        for (Food food : foods) {
            FoodVO foodVO = entityToVO.entitytoFoodvo(food);
            foodVOS.add(foodVO);
        }
        resultVO.setCode(ResStatus.OK);
        resultVO.setMsg("success");
        resultVO.setData(foodVOS);
        return resultVO;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public ResultVO detail(String id) {
        ResultVO resultVO = new ResultVO();
        Food food = new Food();
        food.setFId(Integer.parseInt(id));
        Food food1 = foodMapper.selectOne(food);
        if (food1!=null){
            FoodVO foodVO = entityToVO.entitytoFoodvo(food1);
            foodMapper.updateFClick(food1.getFClick()+1,id);
            resultVO.setCode(ResStatus.OK);
            resultVO.setMsg("success");
            resultVO.setData(foodVO);
        }else {
            resultVO.setCode(ResStatus.NO);
            resultVO.setMsg("没有找到");
            resultVO.setData(null);
        }
        return resultVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultVO add(FoodVO foodVO, List<String> filesPath) {
        Food food=new Food();
        BeanUtils.copyProperties(foodVO,food);

        String filePath = null;
        for (int i = 0; i < filesPath.size(); i++) {
            if (filePath==null){
                filePath=filesPath.get(i);
            }else {
                filePath=filePath +"|"+filesPath.get(i);
            }

        }

        String fContexts = splicing(foodVO.getFContexts());

        String fIngredients = splicing(foodVO.getFIngredients());

        String fKitchenwares = splicing(foodVO.getFKitchenwares());

        food.setFContext(fContexts);
        food.setFIngredients(fIngredients);
        food.setFImage(filePath);
        food.setFClick(0);
        food.setFCreatetime(new Date());
        food.setFKitchenware(fKitchenwares);
        int insert = foodMapper.insert(food);
        ResultVO resultVO = new ResultVO();
        if (insert>0){
            resultVO.setCode(ResStatus.OK);
            resultVO.setMsg("success");
            resultVO.setData(insert);
        }else {
            resultVO.setCode(ResStatus.NO);
            resultVO.setMsg("error");
            resultVO.setData(insert);
        }
        return resultVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultVO delete(int id) {
        ResultVO resultVO = new ResultVO();
        Food food = new Food();
        food.setFId(id);
        int delete = foodMapper.delete(food);
        if (delete>0){
            resultVO.setCode(ResStatus.OK);
            resultVO.setMsg("删除成功");
            resultVO.setData(null);
        }else {
            resultVO.setCode(ResStatus.NO);
            resultVO.setMsg("删除失败");
            resultVO.setData(null);
        }
        return resultVO;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public ResultVO selectbyuser(int id) {
        ResultVO resultVO = new ResultVO();
        Example example=new Example(Food.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("uId",id);
        List<Food> foods = foodMapper.selectByExample(example);

        List<FoodVO> foodVOS=new ArrayList<>();
        for (Food food : foods) {
            FoodVO foodVO = entityToVO.entitytoFoodvo(food);
            foodVOS.add(foodVO);
        }
        if (foods.size()>0){
            resultVO.setCode(ResStatus.OK);
            resultVO.setMsg("sccess");
            resultVO.setData(foodVOS);
        }else {
            resultVO.setCode(ResStatus.NO);
            resultVO.setMsg("还没有发布作品");
            resultVO.setData(foodVOS);
        }
        return resultVO;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public ResultVO selectbyname(FoodVO foodVO1) {
        ResultVO resultVO = new ResultVO();
        Example example=new Example(Food.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("fName",foodVO1.getFName());
        List<Food> foods = foodMapper.selectByExample(example);

        List<FoodVO> foodVOS=new ArrayList<>();
        for (Food food : foods) {
            FoodVO foodVO = entityToVO.entitytoFoodvo(food);
            foodVO.setLiked(isLike(foodVO.getFId(),foodVO1.getUId()));
            foodVOS.add(foodVO);
        }
        if (foods.size()>0){
            resultVO.setCode(ResStatus.OK);
            resultVO.setMsg("sccess");
            resultVO.setData(foodVOS);
        }else {
            resultVO.setCode(ResStatus.NO);
            resultVO.setMsg("还没有相同作品");
            resultVO.setData(foodVOS);
        }
        return resultVO;
    }
    public int isLike(Integer fId, Integer uId) {
        FoodLike foodLike = new FoodLike();
        foodLike.setFId(fId);
        foodLike.setUId(uId);
        FoodLike foodLike1 = foodLikeMapper.selectOne(foodLike);
        if (foodLike1!=null){
            return 1;
        }
        return 0;
    }
    @Override
    public ResultVO like(LikeVo likeVo) {
        int islike = isLike(likeVo.getFId(), likeVo.getUId());
        FoodLike like = new FoodLike();
        BeanUtils.copyProperties(likeVo,like);
        ResultVO resultVO = new ResultVO();
        resultVO.setCode(10000);
        resultVO.setData(null);
        if (islike==1){
            foodLikeMapper.delete(like);
            foodMapper.updateDownLike(likeVo.getFId());
            resultVO.setMsg("取消点赞成功");
        }else {
            foodLikeMapper.insert(like);
            foodMapper.updateaddFlike(likeVo.getFId());
            resultVO.setMsg("点赞成功");
        }
        return resultVO;
    }



    public static String splicing(String[] strings){
        String s=null;
        if (strings!=null){
            for (int i = 0; i < strings.length; i++) {
                if (s==null){
                    s=strings[i];
                }else {
                    s=s+"|"+strings[i];
                }
            }
        }
        return s;
    }
}
